﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using IPCPages;
using MetroFramework.Forms;
using FormPageConfigs.ChildPages;
using System.IO;
using KXmlUIControl;

namespace FormPageConfigs
{
    public partial class Form1 : MetroForm
    {
        private List<PageConfigBase> listChildPages = new List<PageConfigBase>();
        
        private Dictionary<string, PageConfigBase> ChildPages = new Dictionary<string, PageConfigBase>();

        private List<XmlModuleObject> listModules = new List<XmlModuleObject>();

        private XmlConfigApplication _appReader = null;

        private string XmlConfigFolder = "";

        private string XmlConfigFile = "";

        public Form1()
        {
            InitializeComponent();
        }

        public bool LoadConfig()
        {
            string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
            directory = directory.Substring(6);
            DirectoryInfo dir = new DirectoryInfo(directory);
            directory = dir.Parent.Parent.FullName;
            XmlConfigFolder = dir.Parent.FullName + "\\config";
            XmlConfigFile = Path.Combine(directory, "Config", "MainForm.xml");
            if (!File.Exists(XmlConfigFile))
            {
                MessageBox.Show("不存在的配置文件:" + XmlConfigFile);
                return false;
            }
            _appReader = new XmlConfigApplication(XmlConfigFile);
            if (!_appReader.OnReading())
            {
                return false;
            }
            listModules = _appReader.ListModules;
            for (int i = 0; i < listModules.Count; i++)
            {
                string file = string.Format(@"{0}\{1}.xml", XmlConfigFolder, listModules[i].Name);
                XmlConfigModule config = new XmlConfigModule(file);
                if (!File.Exists(file))
                {
                    MessageBox.Show("配置文件不存在:" + file);
                    continue;
                }
                PageConfigWorkflow page = new PageConfigWorkflow(listModules[i]);
                if(page.LoadConfig())
                {
                    ChildPages.Add(listModules[i].Name, page);
                }                      
            }
           
            return true;
        }

        private void AddAppModuleAppTreeNode()
        {
            int index = treeView1.Nodes.IndexOfKey("PageConfigWorkflow");
            TreeNode appNode = treeView1.Nodes["PageConfigWorkflow"];
            if (appNode == null)
            {
                MessageBox.Show("控件加载错误");
                return;
            }
            for (int i = 0; i < listModules.Count; i++)
            {
                XmlModuleObject obj = listModules[i];
                appNode.Nodes.Insert(index, obj.Name, obj.ChineseName);
            }
        }

        private void CreatChildPages()
        {
            //PageConfigAiming page0 = new PageConfigAiming();
            //ChildPages.Add(page0.Name, page0);
            PageConfigNaming page1 = new PageConfigNaming();
            ChildPages.Add(page1.Name, page1);
            //PageConfigWorkflow page2 = new PageConfigWorkflow();
            //ChildPages.Add(page2.Name, page2);
            PageReceiptTaking page3 = new PageReceiptTaking();
            ChildPages.Add(page3.Name, page3);
            PageProductClient page4 = new PageProductClient();
            ChildPages.Add(page4.Name, page4);
            ChildPages.PagePhotoPrinting page5 = new ChildPages.PagePhotoPrinting();
            ChildPages.Add(page5.Name, page5);
            listChildPages = ChildPages.Values.ToList<PageConfigBase>();
        }

        private bool InitChildPages()
        {
            for (int i = 0; i < listChildPages.Count; i++)
            {
                if (!listChildPages[i].LoadConfig())
                {
                    MessageBox.Show(listChildPages[i].ErrorMessagge);
                    return false;
                }
                if (!listChildPages[i].InitCtrls())
                {
                    MessageBox.Show(listChildPages[i].ErrorMessagge);
                    return false;
                }
            }
            return true;
        }

        private void ClearPage()
        {
            this.panel1.Controls.Clear();
        }

        private void AddPage(PageConfigBase page)
        {
            this.panel1.Controls.Clear();
            this.panel1.Controls.Add(page);
        }

        private bool UpdateData()
        {
            for (int i = 0; i < listChildPages.Count; i++)
            {             
                if (!listChildPages[i].UpdateData())
                {
                    MessageBox.Show(listChildPages[i].ErrorMessagge);
                    return false;
                }
            }
            return true;
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (UpdateData())
            {
                this.Close();             
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            CreatChildPages();
            LoadConfig();
            AddAppModuleAppTreeNode();
            if (!InitChildPages())
            {
                Close();
                Application.Exit();
                return;
            }
            treeView1.SelectedNode = treeView1.Nodes[0];
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonApplying_Click(object sender, EventArgs e)
        {
            UpdateData();
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode node = e.Node;
            string pageName = node.Name;
            if (ChildPages.ContainsKey(pageName))
            {
                AddPage(ChildPages[pageName]);
            }
        }
    }
}
